package com.snj.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class SupplierContactDemo {

    // 示例的 SupplierContactVO 类
    static class SupplierContactVO {
        private String contact;
        private String contactTel;

        public SupplierContactVO(String contact, String contactTel) {
            this.contact = contact;
            this.contactTel = contactTel;
        }

        // 生成getter和setter方法
        public String getContact() {
            return contact;
        }

        public void setContact(String contact) {
            this.contact = contact;
        }

        public String getContactTel() {
            return contactTel;
        }

        public void setContactTel(String contactTel) {
            this.contactTel = contactTel;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            SupplierContactVO that = (SupplierContactVO) o;
            return Objects.equals(contact, that.contact) &&
                    Objects.equals(contactTel, that.contactTel);
        }

        @Override
        public int hashCode() {
            return Objects.hash(contact, contactTel);
        }
    }

    public static void main(String[] args) {
        List<SupplierContactVO> supplierContactVOList = new ArrayList<>();

        // 测试数据
        SupplierContactVO newContact1 = new SupplierContactVO("Alice", "1234567890");
        SupplierContactVO newContact2 = new SupplierContactVO("Bob", "0987654321");
        SupplierContactVO newContact3 = new SupplierContactVO("Alice", "1234567890"); // 重复
        SupplierContactVO newContact4 = new SupplierContactVO("Bob", "0987654321");

        addContactIfNotExists(supplierContactVOList, newContact1);
        addContactIfNotExists(supplierContactVOList, newContact2);
        addContactIfNotExists(supplierContactVOList, newContact3); // 不会被添加
        addContactIfNotExists(supplierContactVOList, newContact4);

        // 输出集合中的联系人
        supplierContactVOList.forEach(contact ->
                System.out.println("Contact: " + contact.getContact() + ", Tel: " + contact.getContactTel()));
    }

    public static void addContactIfNotExists(List<SupplierContactVO> contactList, SupplierContactVO newContact) {
        boolean exists = contactList.stream().anyMatch(contact ->
                contact.getContact().equals(newContact.getContact()) &&
                        contact.getContactTel().equals(newContact.getContactTel()));

        if (!exists) {
            contactList.add(newContact);
        }
    }
}